package team.pra.manager.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageHelper;
import team.pra.manager.pojo.Checkin;
import team.pra.manager.pojo.Room;
import team.pra.manager.pojo.RoomType;
import team.pra.manager.service.CheckinService;
import team.pra.manager.service.RoomService;
import team.pra.manager.service.RoomTypeService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预定的CURD
 */
@RestController
@RequestMapping("/checkin")
public class CheckinController {

    @Autowired
    private CheckinService checkinService;
    @Autowired
    RoomService roomService;
    @Autowired
    RoomTypeService roomTypeService;

    @GetMapping("/list")
    @RequiresPermissions({"select"})
    public PageHelper<Checkin> checkins(String token, PageHelper<Checkin> pageHelper,
                                      @RequestParam(name = "search[value]") String searchvalue,
                                      @RequestParam(name = "order[0][column]") Integer orderColunm,
                                      @RequestParam(name = "order[0][dir]") String orderDir,
                                      HttpServletRequest request){
        String columnName = request.getParameter("columns["+orderColunm+"][data]");
        pageHelper.setColumnName(columnName);
        pageHelper.setSearchValue(searchvalue);
        pageHelper.setOrderDir(orderDir);
        checkinService.findCheckinByCondition(pageHelper);
        return pageHelper;
    }
    @PostMapping("/insert")
    @RequiresPermissions({"insert"})
    public ManagerResult insertCheckin(String token,Checkin checkin) {
        return checkinService.insert(checkin);
    }

    @GetMapping("/delete")
    @RequiresPermissions({"delete"})
    public ManagerResult deleteCheckin(String token,Integer id) {
        return checkinService.delete(id);
    }
    @PostMapping("/update")
    @RequiresPermissions({"update"})
    public ManagerResult updateCheckin(String token,Checkin checkin) {
        return checkinService.update(checkin);
    }

    @PostMapping("/toedit")
    @RequiresPermissions({"update"})
    public Map<String,Object> toEdit(String token) {
        Map<String, Object> result = new HashMap<>();
        List<Room> rooms = roomService.findAllRooms();
        List<RoomType> roomTypes = roomTypeService.findAllRoomType();
        result.put("rooms",rooms);
        result.put("roomTypes",roomTypes);
        return result;
    }
}
